Skip to content

Keep the connection on errors at Sync - #813

Merged
josevalim merged 1 commit into
elixir-ecto:masterfrom
agustin2606:fix-error-after-command-complete
Nov 30, 2026
Merged

josevalim merged 1 commit into
elixir-ecto:masterfrom
agustin2606:fix-error-after-command-complete

Conversation

@agustin2606

Copy link
Copy Markdown
Contributor

Fixes #812

A non-fatal error reported at the Sync of a query now returns {:error, %Postgrex.Error{}} and keeps the connection, in both :naive and :strict transaction modes.

Problem

A deferred constraint violated outside an explicit transaction is reported at the implicit commit, after CommandComplete. Postgrex returned the error but also dropped the connection, and with transactions: :strict the connection process crashed.

Postgrex.query!(conn, "CREATE TABLE t (a int UNIQUE DEFERRABLE INITIALLY DEFERRED)", [])

Postgrex.query(conn, "INSERT INTO t VALUES (1), (1)", [])
# before: {:error, %Postgrex.Error{postgres: %{code: :unique_violation}}}
#         + "disconnected: ** (Postgrex.Error) ERROR 23505" and a new backend
#         with transactions: :strict -> FunctionClauseError in handle_msg/3
# after:  {:error, %Postgrex.Error{postgres: %{code: :unique_violation}}}, same connection

Cause

recv_ready/3 turned an ErrorResponse into {:disconnect, err, s} (added in 87bac4b for #454), and recv_strict_ready/4 had no clause for it. The server always follows a non-fatal error with ReadyForQuery, so the stream is still usable.

Fix

  • recv_ready/3 and recv_strict_ready/4 return {:error, err, s, buffer}, the same shape as recv_bind/3 and recv_execute, so callers go through error_ready/4, which reads up to ReadyForQuery.
  • The callers that had no clause for it (reload_ready, reload_closed, close/3) now route it to error_ready/4 too.
  • FATAL/PANIC errors and :disconnect_on_error_codes still disconnect.
  • An error at the Close (the statement_timeout race in the issue) still disconnects: recv_close/3 is shared with the savepoint paths, which send Close plus a simple query without a Sync.

Testing

  • Unnamed and cache_statement queries keep the same backend after a deferred unique violation.
  • The same in transactions: :strict mode.

An ErrorResponse that arrives after CommandComplete, at the Sync of an
extended query (for example a deferred constraint checked at the
implicit commit), is followed by ReadyForQuery like any other error.
Read on to it and return the error instead of disconnecting, and stop
crashing with FunctionClauseError in strict transaction mode.
@josevalim
josevalim merged commit daf1530 into elixir-ecto:master Nov 30, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection is dropped when an ErrorResponse arrives after CommandComplete (at Close or Sync)

2 participants